<?php
declare (strict_types = 1);

namespace app\common\middleware\http;
use app\common\model\notify\MsgNotificationConfig;
use think\facade\Event;

class FormatResponseMiddleware
{
    /**
     * 处理请求
     *
     * @param \think\Request $request
     * @param \Closure       $next
     * @return Response
     */
    public function handle($request, \Closure $next)
    {
        // 执行后续请求处理
        $response = $next($request);
        $uri = $request->baseUrl();
        if($response)
        {
            $MsgNotificationConfig = new MsgNotificationConfig;
            foreach ($MsgNotificationConfig->with('listen')->select() as $item)
            {
                $ParentEvent = ucfirst($item->type);
                foreach ($item->listen as $listen)
                {
                    $Event = ucfirst($listen->event);

                    Event::listen("{$ParentEvent}{$Event}" , 'app\common\listener\Notification\Listen');

                    event("{$ParentEvent}{$Event}",[$response,$listen , $request , $item]);

                }

            }
        }

        return $response;
    }
}
